package com.example.myapplication;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

public class ReminderHelper {


    @TargetApi(Build.VERSION_CODES.S)
    public static void requestExactAlarmPermission(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            Intent intent = new Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM);
            intent.setData(Uri.parse("package:" + context.getPackageName()));
            context.startActivity(intent);
        }
    }

    @TargetApi(Build.VERSION_CODES.S)
    public static boolean hasExactAlarmPermission(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            return alarmManager.canScheduleExactAlarms();
        }
        return true; // Android 12 以下默认有权限
    }

    @SuppressLint("ScheduleExactAlarm")
    public static void scheduleReminder(Context context, long triggerTime, String title, String message, String type) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !hasExactAlarmPermission(context)) {
            Log.e("ReminderHelper", "Exact alarm permission is not granted.");
            requestExactAlarmPermission(context);
            return;
        }
        // 打印时间
        Log.d("ReminderHelper", "triggerTime: " + triggerTime);
        // 打印当前时间
        Log.d("ReminderHelper", "current time: " + System.currentTimeMillis());
        if (triggerTime <= System.currentTimeMillis()) {
            Log.e("ReminderHelper", "Trigger time must be in the future.");
            // toast弹出 上述信息
            Toast.makeText(context, "触发时间必须是未来的时间", Toast.LENGTH_SHORT).show();
            // 改成中文后toast弹出 "触发时间必须是未来的时间。"
            return;
        }
        Intent intent = new Intent(context, ReminderReceiver.class);
        intent.putExtra("title", title);
        intent.putExtra("message", message);
        intent.putExtra("type", type);

        // android 9 以下使用 PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
       // PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int) triggerTime, intent, PendingIntent.FLAG_UPDATE_CURRENT);


        // 添加 FLAG_IMMUTABLE 标志以符合 Android 12 的要求
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                context,
                (int) triggerTime,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (alarmManager != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // 打印
                Log.d("ReminderHelper", "setExactAndAllowWhileIdle");
                alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
            } else {
                // 打印
                Log.d("ReminderHelper", "setExact");
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
            }
        }
    }
}
